//异步回调
let seqNum = 0;//序列号记录

const sendFrame = (frame) => {
  return new Promise((resolve, reject) => {
    frame.seq = seqNum++;
    linkLayer.send(frame, (ack) => {
      if (ack.ok) {
        resolve();
      } else {
        reject(new Error('发送失败'));
      }
    });
  });
};//发送帧

linkLayer.on('frame', ({ seq, data }) => {
  if (!checkCheckSum(data)) {
    return;//检查校验和
  }
  if (!inReceiveWindow(seq)) {
    return;//序列号检查
  }
  receiveWindow.update(seq);
});

const sendAck = (seq) => {
  let ackFrame = {
    type: 'ack',
    seq: seq
  };
  linkLayer.send(ackFrame);
  startAckTimer(seq);
};

const processFrame = ({ data }) => {
  networkLayer.receive(data);
  sendWindow.slide();
  if (bufferFull()) {
    pauseSend();
  }
};